import java.awt.*;
import java.io.*;

class CSucker extends CObject
{
	Anchor[] a;						// start & end points

	float kf,b;

	int ym;

	CSucker(int type,int subtype,int x1,int y1,int x2,int y2)
	{
		super(type,subtype);
		a = new Anchor[2];
		a[0] = new Anchor(x1,y1);
		a[1] = new Anchor(x2,y2);
		init();
	}

	void init()
	{
		xmin = (a[0].x < a[1].x) ? a[0].x : a[1].x;
		xmax = (a[0].x > a[1].x) ? a[0].x : a[1].x;
		ymin = (a[0].y < a[1].y) ? a[0].y : a[1].y;
		ymax = (a[0].y > a[1].y) ? a[0].y : a[1].y;
		x1 = a[0].x;
		y1 = a[0].y;
		x2 = a[1].x;
		y2 = a[1].y;

		ym = ymin - 20;
	}

	boolean checkActive(int mx,int my)
	{
		a[0].active = ((x1 - 8 <= mx) && (x1 + 8 >= mx) && (y1 - 8 <= my));
		a[1].active = ((x2 - 8 <= mx) && (x2 + 8 >= mx) && (y2 - 8 <= my));
	    return	(((x1 - 8 <= mx) && (x1 + 8 >= mx) && (my <= y1) && (ym - 8 <= my)) ||
				 ((x2 - 8 <= mx) && (x2 + 8 >= mx) && (my <= y2) && (ym - 8 <= my)) ||
				 ((xmin - 8 <= mx) && (xmax + 8 >= mx) && (ym - 8 <= my) && (ym + 8 >= my)));
	}

	void draw(Graphics g)
	{
		boolean mode = (type == MainFrame.activeType);
		g.setColor(selected ? Color.red : (mode ? color : color2));
		if (blink || !active || selected)
		{
			g.drawLine(a[0].x,a[0].y,a[0].x,ym);
			g.drawLine(a[0].x,ym,a[1].x,ym);
			g.drawLine(a[1].x,a[1].y,a[1].x,ym);

			a[0].draw(g,mode);
			a[1].draw(g,mode);
		}
		blink = !blink;
	}

	void move(int mx,int my,int start_x,int start_y)
	{
		if (a[0].active) moveAnchor(0,mx,my);
		else
		if (a[1].active) moveAnchor(1,mx,my);
		else
		{
			a[0].move(mx - start_x,my - start_y);
			a[1].move(mx - start_x,my - start_y);
		}
		init();
	}

	void moveAnchor(int index,int mx,int my)
	{
		int dx = mx - a[1 - index].x;
		int dy = my - a[1 - index].y;
		if (Math.abs(dx) >= 10)
		{
   			a[index].x = a[1 - index].x + dx;
   			a[index].y = a[1 - index].y + dy;
   			init();
		}
	}
	void write(DataOutputStream out) throws IOException
	{
		out.writeByte((byte)type);
		out.writeByte((byte)subtype);
		out.writeShort((short)a[0].x);
		out.writeShort((short)a[0].y);
		out.writeShort((short)a[1].x);
		out.writeShort((short)a[1].y);
	}

	static CObject read(DataInputStream in,int type) throws IOException
	{
		int subtype = in.readByte();
		int x1 = in.readShort();
		int y1 = in.readShort();
		int x2 = in.readShort();
		int y2 = in.readShort();
		return new CSucker(type,subtype,x1,y1,x2,y2);
	}
}                
